package copyengine.utils.objectpool.unuse
{
	import flash.geom.Point;
	import flash.utils.Dictionary;

	public class CommonObjectFactory extends BaseObjectFactory
	{
		public static const COMMON_OBJECT_POINT:String = "point";

		protected var objectsPool:Dictionary;

		public function CommonObjectFactory(_maxPool:int)
		{
			super(_maxPool);
			this.objectsPool = new Dictionary(true);
		}

		public function createObjectOfData(_uniqueName:String):Object
		{
			var po:CommonPoolObject = this.createObject(_uniqueName) as CommonPoolObject;

			if (po)
			{
				return po.data;
			}

			return null;
		}

		override public function createObject(_uniqueName:String):IPoolObject
		{
			var objectList:Vector.<CommonPoolObject>;
			if (this.objectsPool[_uniqueName])
			{
				objectList = this.objectsPool[_uniqueName];
				var len:uint = objectList.length;

				for (var i:uint = 0; i < len; i++)
				{
					if (!objectList[i].isUse)
					{
						objectList[i].isUse = true;
						return objectList[i];
					}
				}
			}
			else
			{
				objectList = new Vector.<CommonPoolObject>();
				this.objectsPool[_uniqueName] = objectList;
			}

			if (objectList.length < this.maxPoolsSize)
			{
				var po:CommonPoolObject = new CommonPoolObject();
				po.data = this.create(_uniqueName);
				po.isUse = true;
				objectList.push(po);

				return po;
			}
			// else

			return null;
		}

		public function recycleObjectOfData(_uniqueName:String, _object:Object):void
		{
			if (this.objectsPool[_uniqueName])
			{
				var objectList:Vector.<CommonPoolObject> = this.objectsPool[_uniqueName];
				var len:uint = objectList.length;

				for (var i:uint = 0; i < len; i++)
				{
					if (objectList[i].data == _object)
					{
						objectList[i].isUse = false;
					}
				}
			}
		}

		override public function recycleObject(_uniqueName:String, _object:IPoolObject):void
		{
			if (this.nameIsExist(_uniqueName))
			{
				if (_object is CommonPoolObject)
				{
					(_object as CommonPoolObject).isUse = false;
				}
			}
		}

		override public function nameIsExist(_uniqueName:String):Boolean
		{
			if (_uniqueName == COMMON_OBJECT_POINT)
			{
				return true;
			}

			return false;
		}

		private function create(_uniqueName:String):Object
		{
			switch (_uniqueName)
			{
				case COMMON_OBJECT_POINT:
					return new Point();
			}

			return null;
		}

		override public function disposeFactory():void
		{
			// TODO:: dispose
		}
	}
}